<?php
	require('conn.cfg.php');
	require('functions.php');		

	$msg = '';
	$isAuthenticated = 0;
	if(isset($_SESSION['isAuthenticated'])){
		$isAuthenticated = $_SESSION['isAuthenticated'];	
	}
	
	if(isset($_REQUEST['command']) && $_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
		remove_product($_REQUEST['pid']);
	}
	else if(isset($_REQUEST['command']) && $_REQUEST['command']=='clear'){
		unset($_SESSION['cart']);
	}
	else if(isset($_REQUEST['command']) && $_REQUEST['command']=='update'){
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
                    $fred = intval($_SESSION['cart'][$i]['shipfreq']);
                    $pid = $_SESSION['cart'][$i]['productid'];
                    $q = intval($_REQUEST['product'.$pid])*$fred;
                    if($q > 0 && $q <= 999){
                            $_SESSION['cart'][$i]['qty'] = $q;
                            //fix issues: computing discount for product No2 onlys
                            if ($pid == 2)
                                $_SESSION['cart'][$i]['discount'] = get_discount($q);
                    }
                    else{
                         $msg = 'Some products not updated!, quantity must be a number between 1 and 999';
                    }
		}
	}
        
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>X2PERFORMANCE :: Checkout</title>
<link rel="stylesheet" href="assets/css/reset.css" />
<link rel="stylesheet" href="assets/css/text.css" />
<link rel="stylesheet" href="assets/css/960.css" />
<link rel="stylesheet" href="assets/css/style.css" />
<link rel="SHORTCUT ICON" href="favicon.ico"/>
<script type="text/javascript" src="assets/js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="assets/js/kickstart.js"></script>
<script type="text/javascript" src="assets/js/script00.js"></script>
<script type="text/javascript" src="assets/js/skimlink.js"></script>
<script type="text/javascript" src="assets/js/jquery.cycle.js"></script>
<script type="text/javascript" src="assets/js/slideshow.js"></script>
<link rel="stylesheet" href="assets/css/coda-slider.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script src="assets/js/jquery_002.js" type="text/javascript"></script>
<script src="assets/js/jquery_003.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/jquery.js" type="text/javascript" charset="utf-8"></script>
<!--<script src="assets/js/coda-slider.js" type="text/javascript" charset="utf-8"></script>-->

<!--[if IE 7]>
	<style>
    #bottle{ margin-left:-335px;}
    #nav {height:45px;}
    #nav li { display: inline;margin-bottom:8px;}
    </style>
<![endif]-->
<!-- CuFon: Enables smooth pretty custom font rendering. 100% SEO friendly. To disable, remove this section -->
<script type="text/javascript" src="assets/js/cufon-yui.js"></script>
<script type="text/javascript" src="assets/js/PT_Sans_400-PT_Sans_700-PT_Sans_italic_400-PT_Sans_italic_700.font.js"></script>
<script type="text/javascript" src="assets/js/cuf_run.js"></script>
<!-- CuFon ends -->
</head>
<body>
<div  id="header">
  <div class="container_16">
    <noscript>
    <div class="alert"> In order to be able to view this website, Javascript needs to be active in your web browser. </div>
    </noscript>
    <div class="grid_8"> <a id="logo" href="index.html" title="X2 Performance"></a> </div>
    <div class="grid_3"> <a id="nsf" href="http://nsfsport.com/listings/certified_detail.asp?id=707999" title="NSF Certified for Sport" target="_blank"></a> </div>
    <div class="grid_5" id="topnavigation">
      <ul id="topnav">
        <!--<li><a href="">Login</a></li>
         <li><a>|</a></li>
        <li><a href="">Register</a></li>-->
        <li><span class="viewcart"></span><a href="free_trial.html">View cart</a></li>
      </ul>
    </div>
    <div class="clear"></div>
    <div class="grid_16" id="navigation">
      <ul id="nav">
        <li><a href="index.html" class="home">Home</a></li>
        <li><a href="about_us.html" class="aboutcompany">About Us</a></li>
        <li class="current_page_item current_page"><a href="services.html" class="x2performanceactive">X<sup>2</sup>PERFORMANCE</a></li>
        <li><a href="sport.html" class="sportcertified">sportcertified</a></li>
        <li><a href="contact_us.html" class="contact">Contact Us</a></li>
        <li><a href="blog.html" class="blog">Blog</a></li>
      </ul>
      <ul class="order">
        <li><a href="products.php" class="ordernow">Order now</a></li>
      </ul>
    </div>
  </div>
</div>
<div class="container_16">
  <div class="grid_16 innerheader">
    <div class="innerheaderbg"></div>
    <h2>CHECKOUT</h2>
  </div>
  <div class="clear"></div>  
  <div id="X2CardGrid">      
<form name="form1" method="post">
    <input type="hidden" name="pid" />
    <input type="hidden" name="command" />      
<div class="tableContainer">
    <table class="viewCart tabular grid" width="940">
        <tbody>
	<tr>
            <th class="leftAlign">Name</th>
            <th class="rightAlign">Price</th>
            <th class="centerAlign">Qty</th>
            <th class="rightAlign">Total</th>
        </tr>
<script language="javascript">
	function remove_cart(pid){
		if(confirm('Do you really mean to delete this item?')){
			document.form1.pid.value=pid;
			document.form1.command.value='delete';
			document.form1.submit();
		}
	}
	function clear_cart(){
		if(confirm('This will empty your shopping cart, continue?')){
			document.form1.command.value='clear';
			document.form1.submit();
		}
	}
	function update_cart(){
		document.form1.command.value='update';
		document.form1.submit();
	}


</script>
<?php
    if(isset($_SESSION['cart']) && is_array($_SESSION['cart'])){
        $max = count($_SESSION['cart']);
        $discount = 0;
        $qty = 0;
        for($i=0;$i<$max;$i++){
            
            $fred = intval($_SESSION['cart'][$i]['shipfreq']); 
            $pid = $_SESSION['cart'][$i]['productid'];
            $qty = $_SESSION['cart'][$i]['qty'];
            $discount = $_SESSION['cart'][$i]['discount'];
            $pname = get_product_name($pid);
            $pdesc = get_product_short_desc($pid);
            $thumb = get_product_thumb($pid);
            if($qty == 0) continue; 
?>		
        <tr>
            <td class="leftAlign">
                    <img class="cartThumb" src="<?=$thumb?>">
                    <p class="cartProduct"><?=$pname?></p>                    
                    <?php if($fred > 1){?>
                    <p class="x2cartdescs"><?=$pdesc?> </p>
                    <p class="x2cartdescs"><?php echo "$fred Month Plan";?> </p>
                    <?php }else{?>
                    <p class="cartDescription"><?=$pdesc?> </p>
                    <?php }?>
                    <a href="javascript:remove_cart(<?=$pid?>);" style="color: navy;">Remove</a>				
            </td>
            <td class="rightAlign" width="50">$ <?=get_price($pid)?></td>
            <td class="centerAlign">
                <input type="text" size="1" value="<?=$qty/$fred?>" name="product<?=$pid?>" class="qtyField" <?php if($pid == 3) print('readonly');?> />
                    <a class="updateCart" href="javascript:update_cart(<?=$pid?>);">Update</a>														
            </td>
            <td class="rightAlign" width="70">$ <?=number_format(get_price($pid)*$qty/$fred,2)?></td>
        </tr>
<?php
		if($discount > 0){
?>
		<tr>
            <td><p class="cartDiscount">Discount amount</p></td>
            <td></td>
            <td class="centerAlign"></td>
            <td class="rightAlign"><p class="cartDiscount">-$ <?=number_format($qty * $discount/$fred, 2)?></p>
            </td>
        </tr>			
<?php
		}
    }
?>         
        <tr class="subtotal">
            <td class="leftAlign">Sub Total:</td>
            <td class="rightAlign"></td>
            <td class="centerAlign"></td>
            <td class="rightAlign">$ <?=number_format(get_order_total() - get_discount_total(), 2)?></td>
        </tr>
        
<?php
    }
?>        
		</tbody>
	</table>
</div>      
</form>      
  </div>
  
  <!-- link checkout -->
<div class="checkoutLinks">    
<?php if(isset($_SESSION['cart']) && is_array($_SESSION['cart']) && count($_SESSION['cart'])> 0){?>    
    <a class="continueButton" href="checkout.php">Checkout</a>
<?php }?>    
</div>
<div style="color:#F00"><?=$msg?></div>  
</div>
<br clear="all">
<div id="footer">
  <div class="container_16">
    <div id="footersectionone">
      <div class="grid_1">
        <h3><a href="index.html">Home</a></h3>
      </div>
      <div class="grid_3 footermenucontainer">
        <h3><a href="about_us.html">About Us</a></h3>
        <ul class="mainlinks">
          <li><a href="about_us.html#ceomessage">CEO Message</a></li>
        </ul>
      </div>
      <div class="grid_3 footermenucontainer">
        <h3><a href="services.html">X<sup>2</sup>PERFORMANCE</a></h3>
        <ul class="mainlinks">
          <li><a href="services.html#science">Science</a></li>
          <li><a href="supplymentfacts.html">Supplement facts</a></li>
          <li><a href="instruction.html">Instructions</a></li>
          <li><a href="pricing.html">Pricing</a></li>
          <li><a href="testimonial.php">Testimonials</a></li>
        </ul>
      </div>
      
      <div class="grid_2">
        <h3><a href="contact_us.html">Contact Us</a></h3>
        <ul class="mainlinks">
          <li><a href="privacy.html">Privacy Policy</a></li>
          <li><a href="terms.html">Terms</a></li>
        </ul>
      </div>
      <div class="grid_2">
        <h3><a href="sport.html">Sport certified</a></h3>
      </div>
      <div class="grid_1">
        <h3><a href="blog.html">Blog</a></h3>
      </div>
      <div class="grid_5" id="socialicons">
        <h3>CONNECT WITH US</h3>
        <a id="twitter" href="http://twitter.com/#!/x2performance" title="Twitter" target="_blank"></a> <a id="facebook" href="http://www.facebook.com/X2PERFORMANCE" title="Facebook" target="_blank"></a> <a id="youtube" href="http://www.youtube.com/user/TEAMX2PERFORMANCE/featured" title="You Tube" target="_blank"></a> <a id="googleplus" href="https://plus.google.com/109519688499936342953/posts" title="Google Plus" target="_blank"></a> </div>
      <div class="clear"></div>
    </div>
    <div id="footersectiontwo"> <span id="footerlogo"></span>
      <h3><span>MAKE IT HAPPEN <sup>®</sup></span><br/>
        PROUDLY MADE IN THE USA &copy; 2012 Advanced Bio Development LLC</h3>
      <span id="footerlogo2"></span> </div>
  </div>
 
</div>
<script type="text/javascript" src="assets/js/menu.js"></script>
</body>
</html>